$(document).ready(function() {

	function pageLoading() {
		setTimeout(function() {
			var index = layer.load(2, {shade: [0.1,'#fff']});
			setTimeout(function() {
				layer.close(index);
				$('.index').css({'opacity': '1'});
			},100);
		},10);

	}pageLoading();

	// 点击登陆和注册切换
	$('.go-registration').on('click', 'a', function(event) {
		$(this).parents('.login-tab').removeClass('show').siblings().addClass('show')
	});

	// 点击眼睛切换是否隐藏状态
	$('.eye').click(function() {
		var _this = $(this);
		if (_this.hasClass('eye-off')) {
			$(this).addClass('eye-on').removeClass('eye-off').siblings('input').attr('type','password');
		} else {
			$(this).addClass('eye-off').removeClass('eye-on').siblings('input').attr('type','text');
		}
	});

	// 点击登陆
	$('.login-btn').click(function() {
		var username = $('.inp-username');
		var password = $('.inp-password');
		if (username.val() == "") {
			layer.msg('手机号/用户名 不能为空！', {time:1000});
			username.focus();
			return
		}
		if (password.val() == "") {
			layer.msg('密码不能为空！', {time:1000});
			password.focus();
			return
		}
		if (username.val() !== "" && password.val() !== "") {
			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				layer.msg('登陆成功！', {time:1000});
				layer.close(index);


				// ajax 短信验证发送

			},1000);
		};
	})


	// 点击注册
	$('.register-btn').click(function() {

		var phone 	 = $('.inp-phone');
		var mobileReg  = /^(1[0-9])\d{9}$/;
		var message  = $('.inp-message');
		var password = $('.inp-set-password');

		if (phone.val() == "") {
			layer.msg('手机号码不能为空！', {time:1000});
			phone.focus();
			return
		}
		if (!mobileReg.test(phone.val())) {
			layer.msg('手机号码格式不正确！', {time:1000});
			return
		}
		if (message.val() == "") {
			layer.msg('短信验证码不能为空！', {time:1000});
			message.focus();
			return
		}
		if (password.val() == "") {
			layer.msg('请设置您的密码', {time:1000});
			password.focus();
			return
		}
		if (phone.val() !== "" && mobileReg.test(phone.val()) && message.val() !== "" && password.val() !== "") {
			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				layer.msg('注册完成!', {time:1000});
				layer.close(index);
				time();
				$('.register').removeClass('show');
				$('.success-box').addClass('show');

				// ajax 短信验证发送

			},1000);
		};
	});

	
	// 点击获取验证码
	$('.obtain-validation-btn').on('click','a',function () {
		var phone 	 = $('.inp-phone');
		var mobileReg  = /^(1[0-9])\d{9}$/;

		if (phone.val() == "") {
			layer.msg('手机号码不能为空！', {time:1000});
			phone.focus();
			return
		}
		if (!mobileReg.test(phone.val())) {
			layer.msg('手机号码格式不正确！', {time:1000});
			return
		}
		if (phone.val() !== "" && mobileReg.test(phone.val())) {
			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				layer.msg('短信验证发送成功！', {time:1000});
				layer.close(index);
				time();

				// ajax 短信验证发送

			},1000);
		};
	});

	// 60s倒计时
	var wait=60;
	function time() {
		if (wait == 0) {
			$('.obtain-validation-btn a').remove();
			$('.obtain-validation-btn').html("<a>获取验证码</a>");
			wait = 60;
		} else {
			$('.obtain-validation-btn a').remove();
			$('.obtain-validation-btn').html("<b>"+wait+"秒</b>");
			wait--;
			setTimeout(function() {time()},1000)
		}
	};

	// 点击加好友
	$('.r-div .add-friends-btn').click(function() {
		$('.add-friends-wrap textarea').val('');
		$('.add-friends-wrap').fadeIn();
	});

	// 点击确认添加朋友
	$('.add-friends-wrap').on('click','.confirm-btn',function () {
		var _this = $(this);
		var textarea = _this.siblings('textarea');
		if (textarea.val() == '') {
			layer.msg('悄悄说点什么吧~', {time:1000});
			textarea.focus();
		} else {
			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				layer.close(index);
				layer.msg('发送成功~', {time:1000});

				$('.add-friends-wrap').fadeOut();
				return
			},1000);
		}
	})

	$('.corpus-li i').hover(function() {
		$(this).find('ul').fadeIn();
	}, function() {
		$(this).find('ul').fadeOut();
	});

	$('.article-box ul li').click(function(){
		$(this).addClass('select').siblings().removeClass('select').parents('.column').siblings('.content').find('.con-box').eq($(this).index()).addClass('show').siblings().removeClass('show');
	})

	// 点击关注
	var followChange = true;
	$('.r-div .follow-btn').click(function() {
		var _this = $(this);
		if (_this.hasClass('change-state')) {
			_this.removeClass('change-state');
			layer.msg('取消关注！', {time:1000});
			_this.html('关注');
		} else {
			_this.addClass('change-state');
			layer.msg('关注成功！', {time:1000});
			_this.html('已关注');
		}
	});

	$('.position-nav-wrap .follow-btn').click(function() {
		var _this = $(this);
		if (_this.hasClass('change-state')) {
			_this.removeClass('change-state');
			layer.msg('取消关注！', {time:1000});
		} else {
			_this.addClass('change-state');
			layer.msg('关注成功！', {time:1000});
		}
	});

	// 返回顶部
	$('.return-top').click(function(){
		$('html, body').stop().animate({
			'scrollTop' : '0'
		},1000);
	});

	// 书签锚节点动画
	$(".catalog-btn li a").click(function () {
		$("html, body").animate({scrollTop: $($(this).attr("href")).offset().top -40+ "px"}, 500);
		return false;
	});




	// 所有tab切换
	$('.tab-h2 span').click(function(){
		$(this).addClass('select').siblings().removeClass('select').parent('.tab-h2').siblings().children('.box').eq($(this).index()).addClass('show').siblings().removeClass('show');
	})

	// 超过38字符长度做省略号
	function reduce(classA, number) {
		$(classA).each(function(){
			if($(this).text().length>number){
				var str = $(this).text();
				$(this).text(str.substr(0,number)+'...');
			}
		});
	}

	var reduce01 = reduce('.personal-index .reduce-p', 38);
	var reduce02 = reduce('.community-index .reduce-p', 170);
	var reduce03 = reduce('.interlocution-index .reduce-p', 160);

	// 滑动检测
	$(window).scroll(function() {
		var scrollTop = $(this).scrollTop();
		var scrollHeight = $(document).height();
		var windowHeight = $(this).height();
		if ($(document).scrollTop() > 50) {
			$('.header').addClass('header-add');
		} else {
			$('.header').removeClass('header-add');
		}
	});

	// 轮播
	jQuery(".slider").slide({ mainCell:".bd ul",titCell:".ft a",effect:"fold",effect:"fade",autoPlay:true,delayTime:1000,interTime:5000 });

	// 广播
	jQuery(".txtScroll-top").slide({titCell:".hd ul",mainCell:".bd ul",autoPage:true,effect:"top",autoPlay:true,delayTime:500,vis:1});

	// 开启加入学习弹窗
	$('.join-learning-btn').on('click', function () {
		$('.join-learning-wrap').fadeIn();
	});

	// 关闭加入学习弹窗
	$('.learning-box, .resource-module-wrap, .add-friends-wrap, .delete-corpus-wrap, .revise-corpus-wrap').on('click', '.close-btn', function () {
		$('.join-learning-wrap, .resource-module-wrap, .add-friends-wrap, .delete-corpus-wrap, .revise-corpus-wrap').fadeOut();
	});

	// 发表评论
	$('.publish-btn').click(function() {
		var textarea = $('.publish-comment textarea');
		if(textarea.val() == "") {
			textarea.focus();
			layer.msg('评论不能为空', {time:1000});
			return
		}
		if(textarea.val().length <= 12) {
			textarea.focus();
			layer.msg('评论不能少于12个字符', {time:1000});
			return
		}
		if (textarea.val() !== "" && textarea.val().length > 12) {
			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				layer.msg('提交审核中', {time:1000});
				layer.close(index);

				console.log(textarea.val());


				var addHtml = '<li>' +
							'<dl>' +
								'<dt>' +
									'<img src="images/pic_img.png">' +
								'</dt>' +
								'<dd>' +
									'<h6><strong>我是女神</strong><b>1分钟前</b></h6>' +
									'<div class="comment-inf"><p>'+textarea.val()+'</p></div>' +
									'<a class="reply-btn" href="javascript:;" title="回复">回复</a>' +
								'</dd>' +
							'</dl>' +
							'<label class="reply-label" for="">' +
								'<input type="text" name="" value="">' +
								'<a href="javascript:;" title="提交">提交</a>' +
							'</label>' +
						'</li>';
				$('.news-comment ul').prepend(addHtml);

				textarea.val('')

				return
			},1000);

			// ajax ....
		};
	});

	// 点击开启回复框
	$('.news-comment ul').on('click', 'li .reply-btn', function() {
		$(this).parent('dd').parent('dl').siblings('.reply-label').css({
			'display': 'block'
		}).find('input').focus().val('');
	});

	$('.answer-content-wrap ul').on('click', 'li .reply-btn a', function() {
		$(this).parent('.reply-btn').siblings('.reply-label').css({
			'display': 'block'
		}).find('input').focus().val('');
	});
	

	// 失焦关闭
	$('.news-comment ul, .answer-content-wrap ul').on('blur', 'li .reply-label input', function() {
		$(this).parent('label').fadeOut();
	});

	// 点击提交信息
	$('.news-comment ul').on('blur', 'li .reply-label a', function() {
		var addVal = $(this).siblings('input');
		var addReply = ''
		if (addVal.val().length <= 6) {
			layer.msg('评论不能少于6个字符', {time:1000});
		} else {
			addReply = '<p>我是女神回复：'+addVal.val()+'</p>';
			$(this).parents('li').find('dd').find('div').append(addReply);
		}
	});

	$('.answer-content-wrap ul').on('blur', 'li .reply-label a', function() {

		var addVal = $(this).siblings('input');
		var addReply = ''
		if (addVal.val().length <= 6) {
			layer.msg('评论不能少于6个字符', {time:1000});
		} else {
			addReply = '<p>我是女神回复：'+addVal.val()+'</p>';
			$(this).parents('li').find('.right-p-box').find('.comment-inf').append(addReply);
		}
	});



	// 点击开启资源模块
	$('.learning-resource-box').on('click', '.resource-btn', function () {
		$('.resource-module-wrap').fadeIn();
	})

	// 提交资源模块信息
	$('.resource-module-wrap').on('click','.confirm-btn',function () {
		var module 	  = $('.select-module').find("option:selected").val();
		var category  = $('.select-category').find("option:selected").val();
		var other 	  = $('.label-other input').val();
		var name 	  = $('.label-name input').val();
		var textarea  = $('.textarea-introduce').val();

		if (module == '0') {
			layer.msg('请选择资源模块！', {time:1000});
			$('.select-module').focus();
			return
		}
		if (category == '0') {
			layer.msg('请选择资源类别!', {time:1000});
			$('.select-category').focus();
			return
		}
		if (name == '') {
			layer.msg('请输入资源名称', {time:1000});
			$('.label-name input').focus();
			return
		}
		if (textarea == '') {
			layer.msg('请输入资源介绍!', {time:1000});
			$('.textarea-introduce').focus();
			return
		}
		if(textarea.length <= 12) {
			layer.msg('评论不能少于12个字符', {time:1000});
			$('.textarea-introduce').focus();
			return
		}
		if (module !== '0' && category !== '0' && name !== '' && textarea !== '' && textarea.length > 12) {
			console.log(module);
			console.log(other);
			console.log(category);
			console.log(name);
			console.log(textarea);


			var index = layer.load(2, {
				shade: [0.1,'#fff']
			});
			setTimeout(function() {
				$('.resource-module-wrap').fadeOut();
				layer.msg('提交审核中', {time:1000});
				layer.close(index);
				return
			},1000);

			// ajax ....
		}
	});

	// 资源模块 其他输入
	$('.select-module').change(function () {
		var module 	  = $('.select-module').find("option:selected").val();
		if (module == '9') {
			$('.label-other').addClass('show');
		};
	})


	// 点击展开
	$('.right-p-box').on('click','.more-btn',function() {
		var _this = $(this);
		_this.siblings('p').css({
			'height': 'auto'
		});
		_this.remove();
	})


	// 点击编辑
	$('.corpus-li').on('click','.revise-btn',function() {
		var dataNum = $(this).parents('.corpus-li').attr('data');
		var textP = $(this).parents('.corpus-li').find('p').text();
		$('.revise-corpus-wrap').attr('data', dataNum);
		$('.revise-corpus-wrap').fadeIn().find('input').val(textP);;
	})

	// 编辑确认
	$('.revise-box').on('click','.confirm-btn',function() {
		var inpText = $(this).siblings('label').find('input').val();;
		var dataNum = $('.revise-corpus-wrap').attr('data');
		$('.revise-corpus-wrap').fadeOut();
		$('.corpus-li-'+dataNum+'').find('p').html(inpText);
	})

	// 点击删除
	$('.corpus-li').on('click','.delete-btn',function() {
		var dataNum = $(this).parents('.corpus-li').attr('data');
		var textP = $(this).parents('.corpus-li').find('p').text();
		$('.delete-corpus-wrap').attr('data', dataNum);
		$('.delete-corpus-wrap').fadeIn().find('span').html(textP);
	})

	// 删除确认
	$('.delete-box').on('click','.confirm-btn',function() {
		var dataNum = $('.delete-corpus-wrap').attr('data');
		$('.delete-corpus-wrap').fadeOut();
		$('.corpus-li-'+dataNum+'').remove();
	})

})



// 注册
function register() {
	$('.login-wrap').fadeIn();
	$('.login-box').addClass('show');
	$('.register-box').removeClass('show');
}

// 登录
function login() {
	$('.login-wrap').fadeIn();
	$('.register-box').addClass('show');
	$('.login-box').removeClass('show');
}

// 关闭 登陆&注册 弹窗
$('.login-wrap .close-btn').click(function () {
	$('.login-wrap').fadeOut();
})

// 意见反馈选择按钮
$('.feedback-wrap .choice-box ul').on('click','i',function () {
	var _this = $(this);
	if (_this.hasClass('on')) {
		_this.removeClass('on');
	} else {
		_this.addClass('on');
	}
})

// 开启意见反馈
$('.index').on('click','.feedback-open',function () {
	$('.feedback-wrap').fadeIn();
})

// 关闭意见反馈
$('.feedback-wrap .btn-box').on('click','.cancel-btn',function () {
	$('.feedback-wrap').fadeOut();
})

// 关闭意见反馈
$('.feedback-wrap').on('click','.complete-btn',function () {
	$('.feedback-wrap').fadeOut();
	$('.success-box').removeClass('show');
	$('.feedback-box').addClass('show');
})

// 关闭意见反馈
$('.feedback-wrap .btn-box').on('click','.confirm-btn',function () {
	var selectI		 = $('.choice-box li i');
	var textareaX 	 = $('.textarea-box textarea');
	var verification = $('.verification-box input');
	if (!selectI.hasClass('on')) {
		layer.msg('请选择需要反馈的选项！', {time:1000});
		return
	}
	if (textareaX.val() == '') {
		layer.msg('意见不能为空哦！', {time:1000});
		textareaX.focus();
		return
	}
	if (verification.val() == '') {
		layer.msg('验证码不能为空！', {time:1000});
		verification.focus();
		return
	}
	if (selectI.hasClass('on') && textareaX.val() !== '' && verification.val() !== '') {
		var index = layer.load(2, {
			shade: [0.1,'#fff']
		});
		setTimeout(function() {
			selectI.removeClass('on');
			textareaX.val('');
			verification.val('');

			return
		},500);

		setTimeout(function() {
			layer.msg('反馈成功！', {time:1000});
			layer.close(index);
			$('.feedback-box').removeClass('show');
			$('.success-box').addClass('show');
			return
		},1000);
	};
})

// 星星等级效果
function starsX() {
	layui.use(['rate'], function(){
		var rate = layui.rate;
		//显示文字
		function stars(classX) {
			rate.render({
				elem: classX
				,value: 5
			});
		}
		var stars01 = stars('.stars-01');
		var stars02 = stars('.stars-02');
		var stars03 = stars('.stars-03');
		var stars04 = stars('.stars-04');
	});
}

// 编辑器功能
function editor() {
	var E = window.wangEditor
	var editor2 = new E('#div3')
	editor2.customConfig.menus = [
		'head',
		'bold',
		'italic',
		'underline'
	]
	editor2.create()

	document.getElementById('btn2').addEventListener('click', function () {
		alert(editor2.txt.text())
	}, false)
}


// 点赞功能
$('.answer-content-wrap li, .forum-feedback-wrap li').on('click', '.praise', function(event) {
	var _this = $(this);
	var zanNum= Number(_this.find('b').html());
	if (_this.hasClass('zan-02')) {
		_this.addClass('zan-01').removeClass('zan-02').find('.aniAdd1').fadeIn().addClass('zan-ani').siblings('b').html(zanNum + 1);
		setTimeout(function() {
			_this.find('.aniAdd1').fadeOut().removeClass('zan-ani');
		},1000);
	} else {
		layer.msg('您已点过赞了哦~', {time:1000});
	}
});
